﻿/***********************************************************************************************
COPYRIGHT 2008 Vijeth D

This file is part of Handwritten Character Recognition - NeuronDotNet Sample.
(Project Website : http://neurondotnet.freehostia.com)

NeuronDotNet is a free software. You can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

NeuronDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with NeuronDotNet.
If not, see <http://www.gnu.org/licenses/>.

***********************************************************************************************/

using System;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using NeuronDotNet.Controls;
using NeuronDotNet.Core;
using NeuronDotNet.Core.Backpropagation;

namespace NeuronDotNet.Samples.CharacterRecognition
{
    public partial class MainForm : Form
    {
        Alphabet currentLetter = null;
        private static readonly string[] letters = 
        {
            "A", "a", "B", "b", "c", "C", "d","Đ", "D", 
            "đ", "e", "E", "F", "f", "G", "g","h", "H",
            "I", "J", "K", "k", "L", "l" ,"m","M", "N",
            "n", "o", "P", "Q", "q", "R" ,"r","S", "T",
            "t", "u", "Ư", "U", "ư", "V" ,"W","X", "Y",
            "y", "z", 
            // tahoma
            "A" ,"a" ,"B" ,"b", "c", "d", "D", "Đ", "đ", "e",
            "E" ,"F" ,"f" ,"G", "g", "h", "H", "I", "J", "K",
            "k" ,"L" ,"m" ,"M" ,"N", "n", "o", "p", "P", "Q",
            "q" ,"R" ,"r" ,"S" ,"T", "t", "u", "ư", "U", "Ư",
            "v" ,"W" ,"X" ,"Y" ,"y", "z", "l", "p",
            //times new roman sign
            "`","/","~","?","^","_","A", "B", "C", "D", "E",
            "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z"


        };/*
        private static readonly string[] letters = 
        {
            "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z"
        };*/
        private Bitmap bm;
        private Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> frame;

        public MainForm()
        {
            InitializeComponent();
        }

        private void LoadLetter(object sender, EventArgs e)
        {
            currentLetter = Alphabet.GetLetter(cboAplhabet.SelectedIndex);
            SetLabels();
        }

        public void AddInstance(Bitmap bimap, int LetterIndex)
        {
            Alphabet t = Alphabet.GetLetter(LetterIndex);
            t.AddInstance(getInstanceFromImage(bimap));
        }
        private void AddInstance(object sender, EventArgs e)
        {
            currentLetter.AddInstance(picTraining.Letter);
            SetLabels();
        }

        private void RemoveInstance(object sender, EventArgs e)
        {
            currentLetter.RemoveCurrentInstance();
            SetLabels();
        }

        private void RemoveAll(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Confirm Removal", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                currentLetter.RemoveAll();
                SetLabels();
            }
        }

        private void ClearImage(object sender, EventArgs e)
        {
            btnClearImage.Enabled = false;
            btnAdd.Enabled = false;
            btnRemove.Enabled = false;
            picTraining.Letter = new Letter();
            picTraining.Invalidate();
            lblOf.Text = "New";
        }

        private void MoveNext(object sender, EventArgs e)
        {
            currentLetter.MoveNext();
            SetLabels();
        }

        private void MovePrevious(object sender, EventArgs e)
        {
            currentLetter.MovePrevious();
            SetLabels();
        }

        private void SetLabels()
        {
            if (this.currentLetter.InstancesCount > 0)
            {
                lblOf.Text = (currentLetter.InstanceIndex + 1) + " of " + currentLetter.InstancesCount.ToString();
                btnPrev.Enabled = true;
                btnNext.Enabled = true;
                btnClearImage.Enabled = true;
                btnRemoveAll.Enabled = true;
                btnRemove.Enabled = true;
                btnAdd.Enabled = true;
                picTraining.Letter = currentLetter.CurrentInstance;
            }
            else
            {
                lblOf.Text = "0 of 0";
                btnPrev.Enabled = false;
                btnNext.Enabled = false;
                btnClearImage.Enabled = false;
                btnRemove.Enabled = false;
                btnRemoveAll.Enabled = false;
                btnAdd.Enabled = false;
                picTraining.Letter = new Letter();
            }
            picTraining.Invalidate();
        }

        private void Clear(object sender, EventArgs e)
        {
            picCompressed.Invalidate();
            picRecognition.Letter = new Letter();
            picRecognition.Invalidate();
            lblResult.Visible = false;
            lblPreResult.Visible = false;
        }

        private void TrainingPicMousedown(object sender, MouseEventArgs e)
        {
            btnAdd.Enabled = true;
            btnRemove.Enabled = false;
            btnClearImage.Enabled = true;
            lblOf.Text = "New";
        }

        private void RecognitionPicMousedown(object sender, MouseEventArgs e)
        {
            lblResult.Visible = false;
            lblPreResult.Visible = false;
        }

        private void Train(object sender, EventArgs e)
        {
            btnTrain.Enabled = false;

            int cycles = 200;
            if (!int.TryParse(txtCycles.Text, out cycles)) { cycles = 200; }
            txtCycles.Text = cycles.ToString();

            int currentCombination = 0;
            int totalCombinations = Alphabet.LetterCount * (Alphabet.LetterCount - 1) / 2;

            for (int i = 0; i < Alphabet.LetterCount; i++)
            {
                for (int j = i + 1; j < Alphabet.LetterCount; j++)
                {
                    ActivationLayer inputLayer = new LinearLayer(400);
                    ActivationLayer hiddenLayer = new SigmoidLayer(4);
                    ActivationLayer outputLayer = new SigmoidLayer(2);
                    new BackpropagationConnector(inputLayer, hiddenLayer);
                    new BackpropagationConnector(hiddenLayer, outputLayer);
                    BackpropagationNetwork network = new BackpropagationNetwork(inputLayer, outputLayer);

                    TrainingSet trainingSet = new TrainingSet(400, 2);
                    Alphabet ithLetter = Alphabet.GetLetter(i);
                    Alphabet jthLetter = Alphabet.GetLetter(j);
                    foreach (Letter instance in ithLetter.Instances)
                    {
                        trainingSet.Add(new TrainingSample(instance.GetEquivalentVector(20, 20), new double[] { 1d, 0d }));
                    }
                    foreach (Letter instance in jthLetter.Instances)
                    {
                        trainingSet.Add(new TrainingSample(instance.GetEquivalentVector(20, 20), new double[] { 0d, 1d }));
                    }

                    progressTraining.Value = 100 * currentCombination / totalCombinations;

                    Application.DoEvents();

                    bool correct = false;

                    int currentCycles = 35;
                    int count = trainingSet.TrainingSampleCount;

                    while (correct == false & currentCycles <= cycles)
                    {
                        network.Initialize();
                        network.Learn(trainingSet, currentCycles);
                        correct = true;
                        for (int sampleIndex = 0; sampleIndex < count; sampleIndex++)
                        {
                            double[] op = network.Run(trainingSet[sampleIndex].InputVector);
                            if (((trainingSet[sampleIndex].OutputVector[0] > trainingSet[sampleIndex].OutputVector[1]) && op[0] - op[1] < 0.4) || ((trainingSet[sampleIndex].OutputVector[0] < trainingSet[sampleIndex].OutputVector[1]) && op[1] - op[0] < 0.4))
                            {
                                correct = false;
                                trainingSet.Add(trainingSet[sampleIndex]);
                            }
                        }
                        currentCycles *= 2;
                    }

                    lstLog.Items.Add(cboAplhabet.Items[i] + " & " + cboAplhabet.Items[j] + " = " + network.MeanSquaredError.ToString("0.0000"));
                    lstLog.TopIndex = lstLog.Items.Count - (int)(lstLog.Height / lstLog.ItemHeight);
                    try
                    {
                        using (Stream stream = File.Open(Application.StartupPath + @"\Networks\" + i.ToString("00") + j.ToString("00") + ".ndn", FileMode.Create))
                        {
                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(stream, network);
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Failed to save trained neural networks", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    currentCombination++;
                }
            }
            progressTraining.Value = 0;
            btnTrain.Enabled = false;
        }
        public int Classify(double[] input)
        {
            int winner = 0;
            int current = 1;



            while (current < Alphabet.LetterCount)
            {
                try
                {
                    using (Stream stream = File.Open(Application.StartupPath + @"\Networks\" + winner.ToString("00") + current.ToString("00") + ".ndn", FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        IFormatter formatter = new BinaryFormatter();
                        INetwork network = (INetwork)formatter.Deserialize(stream);

                        double[] output = network.Run(input);
                        string result = letters[winner] + " vs " + letters[current] + " = ";
                        if (output[1] > output[0])
                        {
                            winner = current;
                        }
                        result += letters[winner];
                        lstSimilarityTests.Items.Add(result);
                        lstSimilarityTests.TopIndex = lstSimilarityTests.Items.Count - (int)(lstSimilarityTests.Height / lstSimilarityTests.ItemHeight);
                    }
                    current++;

                }
                catch (Exception)
                {
                    MessageBox.Show("Failed to load saved neural networks", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return int.MaxValue;
                }

            }
            return winner;
        }

        public int Classify(System.Drawing.Bitmap bitmap)
        {
            return Classify(getEquivalentVectorFromImage(20, 20, bitmap));
        }
        private void Recognize(object sender, EventArgs e)
        {
            lblResult.Visible = false;
            lblPreResult.Visible = false;
            Graphics g = bufferedPanel1.CreateGraphics();
            g.DrawImage(bm, new Point(0, 0));

            //double[] input = picRecognition.Letter.GetEquivalentVector(20, 20);
            double[] input = getEquivalentVectorFromImage(20, 20, bm);
            //currentLetter.AddInstance(lt);
            int winner = 0;
            int current = 1;

            lstSimilarityTests.Items.Clear();
            picCompressed.Invalidate();
            lblResult.Text = "";


            while (current < Alphabet.LetterCount)
            {
                try
                {
                    using (Stream stream = File.Open(Application.StartupPath + @"\Networks\" + winner.ToString("00") + current.ToString("00") + ".ndn", FileMode.Open))
                    {
                        IFormatter formatter = new BinaryFormatter();
                        INetwork network = (INetwork)formatter.Deserialize(stream);

                        double[] output = network.Run(input);
                        string result = letters[winner] + " vs " + letters[current] + " = ";
                        if (output[1] > output[0])
                        {
                            winner = current;
                        }
                        result += letters[winner];
                        lstSimilarityTests.Items.Add(result);
                        lstSimilarityTests.TopIndex = lstSimilarityTests.Items.Count - (int)(lstSimilarityTests.Height / lstSimilarityTests.ItemHeight);
                    }
                    current++;
                }
                catch (Exception)
                {
                    MessageBox.Show("Failed to load saved neural networks", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            lblResult.Text = letters[Classify(bm)];
            lblResult.Visible = true;
            int c = Classify(bm);
            lblPreResult.Visible = true;
        }

        private void LoadForm(object sender, EventArgs e)
        {
            cboAplhabet.Items.AddRange(letters);
            cboAplhabet.SelectedIndex = 0;
        }
        public Letter getInstanceFromImage(Bitmap bm2)
        {
            Letter lt = new Letter();
            Stroke str;
            int bmHeight = bm2.Height;
            int bmWidth = bm2.Width;
            //System.Drawing.Bitmap bm = new System.Drawing.Bitmap("24sa.bmp");
            Bitmap bm = bm2.Clone(new Rectangle(0, 0, bmWidth, bmHeight), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            for (int i = 0; i < bmHeight; i++)
                for (int j = 0; j < bmWidth; j++)
                    if (bm.GetPixel(j, i) == Color.FromArgb(255, 255, 255))
                    {
                        str = new Stroke();


                        if (bm.GetPixel(j - 1, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j + 1, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j + 1, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j - 1, i + 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i + 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j - 1, i) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j + 1, i) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j + 1, i));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j, i + 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j, i + 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        str.AddPoint(new Point(j, i));
                        lt.AddStroke(str);
                        str = null;
                    }
            return lt;
        }
        private double[] getEquivalentVectorFromImage(int row, int col, Bitmap bm2)
        {
            Letter lt = new Letter();
            Stroke str;
            int bmHeight = bm2.Height;
            int bmWidth = bm2.Width;
            //System.Drawing.Bitmap bm = new System.Drawing.Bitmap("24sa.bmp");
            Bitmap bm = bm2.Clone(new Rectangle(0, 0, bmWidth, bmHeight), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            for (int i = 0; i < bmHeight; i++)
                for (int j = 0; j < bmWidth; j++)
                    if (bm.GetPixel(j, i) == Color.FromArgb(255, 255, 255))
                    {
                        str = new Stroke();


                        if (bm.GetPixel(j - 1, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j + 1, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j + 1, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j - 1, i + 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i + 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j - 1, i) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j - 1, i));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j, i - 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j, i - 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j + 1, i) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j + 1, i));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        if (bm.GetPixel(j, i + 1) == Color.FromArgb(255, 255, 255))
                        {
                            str.AddPoint(new Point(j, i));
                            str.AddPoint(new Point(j, i + 1));
                            lt.AddStroke(str);
                            bm.SetPixel(j, i, Color.FromArgb(0, 0, 0));
                            str = null;
                            continue;
                        }
                        str.AddPoint(new Point(j, i));
                        lt.AddStroke(str);
                        str = null;
                    }

            return lt.GetEquivalentVector(row, col);
        }
        private void DrawCompressed(object sender, PaintEventArgs e)
        {
            Bitmap bms = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(bms);
            Font font = new Font("Tahoma", 10, GraphicsUnit.Point);
            g.DrawString("A A A A WWXYZ", font, Brushes.Black, new PointF(20, 20));
            bms.Save("testfat.png");
            try
            {

                //double[] bitArray = picRecognition.Letter.GetEquivalentVector(20, 20);
                double[] bitArray = getEquivalentVectorFromImage(20, 20, bm);

                using (Bitmap map = new Bitmap(20, 20))
                {
                    int k = 0;
                    for (int i = 0; i < 20; i++)
                    {
                        for (int j = 0; j < 20; j++)
                        {
                            int darkness = (int)((1 - bitArray[k++]) * 255);
                            map.SetPixel(j, i, Color.FromArgb(darkness, darkness, darkness));
                        }
                    }

                    e.Graphics.DrawImage(map, 1, 1, 40, 40);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image|*.bmp;*.png;*.jpg;*.jpeg";
            if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                try
                {
                    Bitmap value = (Bitmap)Bitmap.FromFile(ofd.FileName);
                    if (
                    (value != null) && (value is Bitmap) &&
                    ((value.PixelFormat == System.Drawing.Imaging.PixelFormat.Format16bppGrayScale) ||
                      (value.PixelFormat == System.Drawing.Imaging.PixelFormat.Format48bppRgb) ||
                      (value.PixelFormat == System.Drawing.Imaging.PixelFormat.Format64bppArgb)))
                    {
                        // convert and display image
                        Bitmap tempImage = ConvertImage((Bitmap)value);
                        this.bm = tempImage;

                    }
                    else
                    {
                        // display source image as it is
                        this.bm = value;
                    }
                    bm = value;
                    bm = bm.Clone(new Rectangle(0, 0, bm.Width, bm.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

        }

        private Bitmap ConvertImage(Bitmap source)
        {
            Bitmap newImage = null;
            int layers = 0;

            // get image size
            int width = source.Width;
            int height = source.Height;

            // create new image depending on source image format
            switch (source.PixelFormat)
            {
                case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                    {
                        // create new grayscale image
                        newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                        // get palette
                        System.Drawing.Imaging.ColorPalette cp = newImage.Palette;
                        // init palette with grayscale colors
                        for (int i = 0; i < 256; i++)
                        {
                            cp.Entries[i] = Color.FromArgb(i, i, i);
                        }
                        // set palette back
                        newImage.Palette = cp;

                        layers = 1;
                        break;
                    }

                case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                    {
                        // create new color 24 bpp image
                        newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                        layers = 3;
                        break;
                    }

                case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                    {
                        // create new color 32 bpp image
                        newImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        layers = 4;
                        break;
                    }

                default:
                    return null;
            }

            // lock both images
            System.Drawing.Imaging.BitmapData sourceData = source.LockBits(new Rectangle(0, 0, width, height),
                System.Drawing.Imaging.ImageLockMode.ReadOnly, source.PixelFormat);
            System.Drawing.Imaging.BitmapData newData = newImage.LockBits(new Rectangle(0, 0, width, height),
                System.Drawing.Imaging.ImageLockMode.ReadWrite, newImage.PixelFormat);

            unsafe
            {
                // base pointers
                int sourceBasePtr = (int)sourceData.Scan0.ToPointer();
                int newBasePtr = (int)newData.Scan0.ToPointer();
                // image strides
                int sourceStride = sourceData.Stride;
                int newStride = newData.Stride;

                for (int y = 0; y < height; y++)
                {
                    ushort* sourcePtr = (ushort*)(sourceBasePtr + y * sourceStride);
                    byte* newPtr = (byte*)(newBasePtr + y * newStride);

                    for (int x = 0, lineSize = width * layers; x < lineSize; x++, sourcePtr++, newPtr++)
                    {
                        *newPtr = (byte)(*sourcePtr >> 8);
                    }
                }
            }

            // unlock both image
            source.UnlockBits(sourceData);
            newImage.UnlockBits(newData);

            return newImage;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {


            //OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "Image|*.bmp;*.png;*.jpg;*.jpeg";
            //if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
/*            
                        
      {
          try
          {
              uitocr.imaging.ContourAnalysisNS.ImageProcessor processor = new uitocr.imaging.ContourAnalysisNS.ImageProcessor(new uitocr.imaging.Settings.Setting());
              processor.getBlobcollection = true;
              processor.getskeleton = true;
              processor.NonAdaptiveThreshold = 160;
              processor.analysis = false;
              for (int i = 0 ; i < 26 ;i++ )
              {
                          
                   frame = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>((Bitmap)Bitmap.FromFile((i + 1) +".png"));
                          
                  //Alphabet.GetLetter(i).RemoveAll();

                  //frame = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>((Bitmap)Bitmap.FromFile(ofd.FileName));
                  processor.ProcessImage(frame);
                  int t = 1;
                  //int total = processor.blobCollection.Length;


                  progressBar1.Value = 0;
                  foreach (uitocr.imaging.Blob blob_ in processor.blobCollection)
                  {

                      Alphabet.GetLetter(i).AddInstance(getInstanceFromImage(blob_.Image.ToManagedImage()));

                      t++;
                      //progressBar1.Value = 100 * t / total;
                  }
                  //progressBar1.Value = 0;
                  //label1.Text = letters[i] + "of" + letters[25];
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      
        
      }*/
              

        }
        
    }

    
}
